﻿using EV.EventBus;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ZoneLine.ViewModels;

namespace ZoneLine.Views
{
    /// <summary>
    /// PackageUpdateView.xaml 的交互逻辑
    /// </summary>
    public partial class PackageUpdateView : Window
    {
        public PackageUpdateView()
        {
            InitializeComponent();
            Closed += PackageUpdateView_Closed;
            MessageCenter.Subscribe(nameof(PackageUpdateViewModel), Guid.NewGuid().ToString(), (s) =>
            {
                Op006ShowMsg(s.Source.Payload);

                if (s.Source.Payload is string ss)
                {
                    this.Dispatcher.Invoke(() =>
                    {
                        if (ss.StartsWith("装箱信息"))
                        {
                            if (ss.Contains("成功"))
                            {
                                PackageRlt.Background = new SolidColorBrush(Colors.Green);
                                PackageRlt.Text = ss;
                            }

                            else
                            {
                                PackageRlt.Background = new SolidColorBrush(Colors.Red);
                                PackageRlt.Text = ss;

                                var view = AlarmView.GetView();
                                view.SetInfo(ss, () =>
                                {
                                    if (this.DataContext is PackageBoxViewModel vm)
                                    {
                                        vm.InAlarmState = false;
                                    }
                                });
                                view.WindowStartupLocation = WindowStartupLocation.CenterScreen;
                                view.Topmost = true;
                                view.Show();
                            }
                        }
                    });

                }
                return Task.CompletedTask;
            });
        }

        private void PackageUpdateView_Closed(object sender, EventArgs e)
        {
            MessageCenter.PublishAsync(nameof(PackageUpdateView) + "Closed");
        }

        private int _op006Count = 0;
        private void Op006ShowMsg(object info)
        {
            if (info is string ss)
            {
                Dispatcher.Invoke(() =>
                {
                    _op006Count++;
                    if (_op006Count > 500)
                    {
                        PackageBoxLog.Document.Blocks.Clear();
                        _op006Count = 0;
                    }
                    PackageBoxLog.AppendText(DateTime.Now.ToString("HH:mm:ss") + ss + "\r\n");
                    PackageBoxLog.ScrollToEnd();
                });
            }
        }
    }
}
